/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.common.utils;

import com.thinkgem.jeesite.common.config.Global;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * FreeMarkers工具类
 * @author ThinkGem
 * @version 2013-01-15
 */
public class FreeMarkers {

	public static String renderString(String templateString, Map<String, ?> model) {
		try {
			StringWriter result = new StringWriter();
			Template t = new Template("name", new StringReader(templateString), new Configuration());
			t.process(model, result);
			return result.toString();
		} catch (Exception e) {
			throw Exceptions.unchecked(e);
		}
	}

	public static String renderTemplate(Template template, Object model) {
		try {
			StringWriter result = new StringWriter();
			template.process(model, result);
			return result.toString();
		} catch (Exception e) {
			throw Exceptions.unchecked(e);
		}
	}

	public static Configuration buildConfiguration(String directory) throws IOException {
		Configuration cfg = new Configuration();
		Resource path = new DefaultResourceLoader().getResource(directory);
		cfg.setDirectoryForTemplateLoading(path.getFile());
		return cfg;
	}
	
	public static void main(String[] args) throws IOException {
//		// renderString
//		Map<String, String> model = com.google.common.collect.Maps.newHashMap();
//		model.put("userName", "calvin");
//		String result = FreeMarkers.renderString("hello ${userName}", model);
//		System.out.println(result);
//		// renderTemplate
//		Configuration cfg = FreeMarkers.buildConfiguration("classpath:/");
//		Template template = cfg.getTemplate("testTemplate.ftl");
//		String result2 = FreeMarkers.renderTemplate(template, model);
//		System.out.println(result2);

//		Map<String, String> model = com.google.common.collect.Maps.newHashMap();
//		model.put("userName", "calvin");
//		String result = FreeMarkers.renderString("hello ${userName} ${r'${userName}'}", model);
//		System.out.println(result);



		//生产对应的报告书

		Map<String, String> model1 = new HashMap<String, String>();
		model1.put("ywbh", "2017515165");
//		String result = FreeMarkers.renderString("hello ${userName}", model);
//		System.out.println(result);
//		// renderTemplate
		Configuration cfg = null;
		Template template = null;
		try {
			cfg = FreeMarkers.buildConfiguration("classpath:/");
			template = cfg.getTemplate("notification.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}

		String result2 = FreeMarkers.renderTemplate(template, model1);


		String content = result2;
		String fileDir = Global.getProjectPath() +"htmlPdfFiles" + File.separator;
		String fileName = DateUtils.formatDate(new Date(), "yyyyMMddhhmmss");
		String htmlFileName = fileName + ".html";
		String pdfFileName = fileName + ".pdf";
		String htmlFilePath = fileDir + htmlFileName;
		String pdfFilePath = fileDir + pdfFileName;

		System.out.println(htmlFilePath);

		//生成时间戳的html文件
		FileUtils.createFile(htmlFilePath);
		FileUtils.writeToFile(htmlFilePath, content,"utf-8",true);

		//调用wkhtmltopdf在原文件夹下面进行转换
		String execStr = "wkhtmltopdf " + htmlFilePath + " " + pdfFilePath;
		System.out.println("execStr:  " + execStr);
		Runtime runtime=Runtime.getRuntime();
		try{
			runtime.exec(execStr);
		}catch(Exception e){
			System.out.println("Error!");
		}


	}
	
}
